package org.pp.config;

import cn.hutool.core.io.IoUtil;
import com.wechat.pay.java.core.Config;
import com.wechat.pay.java.core.RSAAutoCertificateConfig;
import lombok.Getter;
import lombok.Setter;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import java.io.InputStream;
import java.nio.charset.StandardCharsets;

@Configuration
@ConfigurationProperties("wx.merchant")
@Component
@Setter
@Getter
public class WechatPayConfig {


    private String merchantId;
    private String merchantSerialNumber;
    private String apiV3Key;
    private String appid;
    private String notifyUrl;

    @Bean
    public RSAAutoCertificateConfig getRSAAutoCertificateConfig(){
        InputStream inputStream = WechatPayConfig.class.getClassLoader()
                .getResourceAsStream("cert/apiclient_key.pem");
        String key = IoUtil.read(IoUtil.getReader(inputStream, StandardCharsets.UTF_8));

        return new RSAAutoCertificateConfig.Builder()
                        .merchantId(merchantId)
                        .privateKey(key)
                        .merchantSerialNumber(merchantSerialNumber)
                        .apiV3Key(apiV3Key)
                        .build();
    }
}
